package com.peakmain.wanandroid.projectmodule.presenter;

import com.peakmain.wanandroid.netmodule.base.BasePresenter;
import com.peakmain.wanandroid.netmodule.bean.DataResponse;
import com.peakmain.wanandroid.netmodule.bean.project.ProjectListBean;
import com.peakmain.wanandroid.netmodule.net.ApiService;
import com.peakmain.wanandroid.netmodule.net.RetrofitManager;
import com.peakmain.wanandroid.netmodule.net.RxSchedulers;
import com.peakmain.wanandroid.projectmodule.contract.ProjectListContract;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;

/**
 * author ：Peakmain
 * createTime：2019/7/13
 * mail:2726449200@qq.com
 * describe：
 */
public class ProjectListPresenter extends BasePresenter<ProjectListContract.View> implements ProjectListContract.Presenter {
    @Inject
    public ProjectListPresenter() {

    }

    @Override
    public void getProjectList(int page, int cid, final boolean isPullDown, final boolean isSilence) {
        mView.showLoading();
        RetrofitManager.create(ApiService.class).getProjectList(page, cid)
                .compose(mView.<DataResponse<ProjectListBean>>bindToLife())
                .compose(RxSchedulers.<DataResponse<ProjectListBean>>applySchedulers())
                .subscribe(new Consumer<DataResponse<ProjectListBean>>() {
                    @Override
                    public void accept(DataResponse<ProjectListBean> dataResponse) throws Exception {
                        if (dataResponse.getErrorCode() == 0) {
                            mView.showProjectListSuccess(dataResponse.getData(),isPullDown,isSilence);
                        } else {
                            mView.showFaild(dataResponse.getErrorMessage());
                        }
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showFaild("网络错误，请稍后重试");
                        mView.hideLoading();
                    }
                });
    }
}
